package craw

import (
	"hotty/craw/tool"
	"strings"

	"github.com/gocolly/colly/v2"
)

const urlForBilibili = "https://www.bilibili.com/v/popular/rank/kichiku"

// Crawer is the tool to craw the htmls
type Crawer struct {
	// colly to craw the message from the website
	collyCraw *colly.Collector
}

// NewCrawer construct the new object of Craw
func NewCrawer() *Crawer {
	innerColly := colly.NewCollector()
	craw := &Crawer{}
	craw.collyCraw = innerColly

	return craw
}

// CrawHotKey is the function for html text
// basic function to test test the function of html text
func (c *Crawer) CrawHotKey(url string) []string {
	//get the init text from weibo
	hotEvents := []string{}
	c.collyCraw.OnHTML("body", func(e *colly.HTMLElement) {
		// improve the process of the result texts
		// get the memes and filter the text to
		// 1. chinese
		// 2. roman number
		// 3. english characters
		e.ForEach(".info > .title", func(_ int, el *colly.HTMLElement) {
			memeKey := tool.StrFilterForSpecChar(el.Text)
			hotEvents = append(hotEvents, memeKey)
		})
	})
	c.collyCraw.Visit(url)

	// with the events to find the memes
	memes := []string{}
	for _, meme := range hotEvents {
		// check if exist the '\n'
		lFlag := strings.Contains(meme, "\n")
		if lFlag == true {
			index := strings.Index(meme, "\n")
			subStr := meme[:index]
			memes = append(memes, subStr)
		}
	}
	return hotEvents
}
